<!doctype html>
<html>
<head>
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>
<title>django教程day01</title><link href='https://fonts.loli.net/css?family=Open+Sans:400italic,700italic,700,400&subset=latin,latin-ext' rel='stylesheet' type='text/css' /><style type='text/css'>html {overflow-x: initial !important;}:root { --bg-color: #ffffff; --text-color: #333333; --select-text-bg-color: #B5D6FC; --select-text-font-color: auto; --monospace: 'Lucida Console',Consolas,'Courier',monospace; }
html { font-size: 14px; background-color: var(--bg-color); color: var(--text-color); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; }
body { margin: 0px; padding: 0px; height: auto; bottom: 0px; top: 0px; left: 0px; right: 0px; font-size: 1rem; line-height: 1.42857143; overflow-x: hidden; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; tab-size: 4; background-position: inherit inherit; background-repeat: inherit inherit; }
iframe { margin: auto; }
a.url { word-break: break-all; }
a:active, a:hover { outline: 0px; }
.in-text-selection, ::selection { text-shadow: none; background: var(--select-text-bg-color); color: var(--select-text-font-color); }
#write { margin: 0px auto; height: auto; width: inherit; word-break: normal; word-wrap: break-word; position: relative; white-space: normal; overflow-x: visible; padding-top: 40px; }
#write.first-line-indent p { text-indent: 2em; }
#write.first-line-indent li p, #write.first-line-indent p * { text-indent: 0px; }
#write.first-line-indent li { margin-left: 2em; }
.for-image #write { padding-left: 8px; padding-right: 8px; }
body.typora-export { padding-left: 30px; padding-right: 30px; }
.typora-export .footnote-line, .typora-export li, .typora-export p { white-space: pre-wrap; }
@media screen and (max-width: 500px) { 
  body.typora-export { padding-left: 0px; padding-right: 0px; }
  #write { padding-left: 20px; padding-right: 20px; }
  .CodeMirror-sizer { margin-left: 0px !important; }
  .CodeMirror-gutters { display: none !important; }
}
#write li > figure:last-child { margin-bottom: 0.5rem; }
#write ol, #write ul { position: relative; }
img { max-width: 100%; vertical-align: middle; }
button, input, select, textarea { color: inherit; font-family: inherit; font-size: inherit; font-style: inherit; font-variant-caps: inherit; font-weight: inherit; line-height: inherit; }
input[type="checkbox"], input[type="radio"] { line-height: normal; padding: 0px; }
*, ::after, ::before { box-sizing: border-box; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p, #write pre { width: inherit; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p { position: relative; }
p { line-height: inherit; }
h1, h2, h3, h4, h5, h6 { break-after: avoid-page; break-inside: avoid; orphans: 2; }
p { orphans: 4; }
h1 { font-size: 2rem; }
h2 { font-size: 1.8rem; }
h3 { font-size: 1.6rem; }
h4 { font-size: 1.4rem; }
h5 { font-size: 1.2rem; }
h6 { font-size: 1rem; }
.md-math-block, .md-rawblock, h1, h2, h3, h4, h5, h6, p { margin-top: 1rem; margin-bottom: 1rem; }
.hidden { display: none; }
.md-blockmeta { color: rgb(204, 204, 204); font-weight: 700; font-style: italic; }
a { cursor: pointer; }
sup.md-footnote { padding: 2px 4px; background-color: rgba(238, 238, 238, 0.701961); color: rgb(85, 85, 85); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; cursor: pointer; }
sup.md-footnote a, sup.md-footnote a:hover { color: inherit; text-transform: inherit; text-decoration: inherit; }
#write input[type="checkbox"] { cursor: pointer; width: inherit; height: inherit; }
figure { overflow-x: auto; margin: 1.2em 0px; max-width: calc(100% + 16px); padding: 0px; }
figure > table { margin: 0px !important; }
tr { break-inside: avoid; break-after: auto; }
thead { display: table-header-group; }
table { border-collapse: collapse; border-spacing: 0px; width: 100%; overflow: auto; break-inside: auto; text-align: left; }
table.md-table td { min-width: 32px; }
.CodeMirror-gutters { border-right-width: 0px; background-color: inherit; }
.CodeMirror-linenumber { }
.CodeMirror { text-align: left; }
.CodeMirror-placeholder { opacity: 0.3; }
.CodeMirror pre { padding: 0px 4px; }
.CodeMirror-lines { padding: 0px; }
div.hr:focus { cursor: none; }
#write pre { white-space: pre-wrap; }
#write.fences-no-line-wrapping pre { white-space: pre; }
#write pre.ty-contain-cm { white-space: normal; }
.CodeMirror-gutters { margin-right: 4px; }
.md-fences { font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; overflow: visible; white-space: pre; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; position: relative !important; background-position: inherit inherit; background-repeat: inherit inherit; }
.md-diagram-panel { width: 100%; margin-top: 10px; text-align: center; padding-top: 0px; padding-bottom: 8px; overflow-x: auto; }
#write .md-fences.mock-cm { white-space: pre-wrap; }
.md-fences.md-fences-with-lineno { padding-left: 0px; }
#write.fences-no-line-wrapping .md-fences.mock-cm { white-space: pre; overflow-x: auto; }
.md-fences.mock-cm.md-fences-with-lineno { padding-left: 8px; }
.CodeMirror-line, twitterwidget { break-inside: avoid; }
.footnotes { opacity: 0.8; font-size: 0.9rem; margin-top: 1em; margin-bottom: 1em; }
.footnotes + .footnotes { margin-top: 0px; }
.md-reset { margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: top; text-decoration: none; text-shadow: none; float: none; position: static; width: auto; height: auto; white-space: nowrap; cursor: inherit; line-height: normal; font-weight: 400; text-align: left; box-sizing: content-box; direction: ltr; background-position: 0px 0px; background-repeat: initial initial; }
li div { padding-top: 0px; }
blockquote { margin: 1rem 0px; }
li .mathjax-block, li p { margin: 0.5rem 0px; }
li { margin: 0px; position: relative; }
blockquote > :last-child { margin-bottom: 0px; }
blockquote > :first-child, li > :first-child { margin-top: 0px; }
.footnotes-area { color: rgb(136, 136, 136); margin-top: 0.714rem; padding-bottom: 0.143rem; white-space: normal; }
#write .footnote-line { white-space: pre-wrap; }
@media print { 
  body, html { border: 1px solid transparent; height: 99%; break-after: avoid-page; break-before: avoid-page; }
  #write { margin-top: 0px; padding-top: 0px; border-color: transparent !important; }
  .typora-export * { -webkit-print-color-adjust: exact; }
  html.blink-to-pdf { font-size: 13px; }
  .typora-export #write { padding-left: 32px; padding-right: 32px; padding-bottom: 0px; break-after: avoid-page; }
  .typora-export #write::after { height: 0px; }
}
.footnote-line { margin-top: 0.714em; font-size: 0.7em; }
a img, img a { cursor: pointer; }
pre.md-meta-block { font-size: 0.8rem; min-height: 0.8rem; white-space: pre-wrap; background-color: rgb(204, 204, 204); display: block; overflow-x: hidden; background-position: initial initial; background-repeat: initial initial; }
p > .md-image:only-child:not(.md-img-error) img, p > img:only-child { display: block; margin: auto; }
p > .md-image:only-child { display: inline-block; width: 100%; }
#write .MathJax_Display { margin: 0.8em 0px 0px; }
.md-math-block { width: 100%; }
.md-math-block:not(:empty)::after { display: none; }
[contenteditable="true"]:active, [contenteditable="true"]:focus { outline: 0px; box-shadow: none; }
.md-task-list-item { position: relative; list-style-type: none; }
.task-list-item.md-task-list-item { padding-left: 0px; }
.md-task-list-item > input { position: absolute; top: 0px; left: 0px; margin-left: -1.2em; margin-top: calc(1em - 10px); border: none; }
.math { font-size: 1rem; }
.md-toc { min-height: 3.58rem; position: relative; font-size: 0.9rem; border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; }
.md-toc-content { position: relative; margin-left: 0px; }
.md-toc-content::after, .md-toc::after { display: none; }
.md-toc-item { display: block; color: rgb(65, 131, 196); }
.md-toc-item a { text-decoration: none; }
.md-toc-inner:hover { text-decoration: underline; }
.md-toc-inner { display: inline-block; cursor: pointer; }
.md-toc-h1 .md-toc-inner { margin-left: 0px; font-weight: 700; }
.md-toc-h2 .md-toc-inner { margin-left: 2em; }
.md-toc-h3 .md-toc-inner { margin-left: 4em; }
.md-toc-h4 .md-toc-inner { margin-left: 6em; }
.md-toc-h5 .md-toc-inner { margin-left: 8em; }
.md-toc-h6 .md-toc-inner { margin-left: 10em; }
@media screen and (max-width: 48em) { 
  .md-toc-h3 .md-toc-inner { margin-left: 3.5em; }
  .md-toc-h4 .md-toc-inner { margin-left: 5em; }
  .md-toc-h5 .md-toc-inner { margin-left: 6.5em; }
  .md-toc-h6 .md-toc-inner { margin-left: 8em; }
}
a.md-toc-inner { font-size: inherit; font-style: inherit; font-weight: inherit; line-height: inherit; }
.footnote-line a:not(.reversefootnote) { color: inherit; }
.md-attr { display: none; }
.md-fn-count::after { content: '.'; }
code, pre, samp, tt { font-family: var(--monospace); }
kbd { margin: 0px 0.1em; padding: 0.1em 0.6em; font-size: 0.8em; color: rgb(36, 39, 41); background-color: rgb(255, 255, 255); border: 1px solid rgb(173, 179, 185); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; box-shadow: rgba(12, 13, 14, 0.2) 0px 1px 0px, rgb(255, 255, 255) 0px 0px 0px 2px inset; white-space: nowrap; vertical-align: middle; background-position: initial initial; background-repeat: initial initial; }
.md-comment { color: rgb(162, 127, 3); opacity: 0.8; font-family: var(--monospace); }
code { text-align: left; }
a.md-print-anchor { white-space: pre !important; border: none !important; display: inline-block !important; position: absolute !important; width: 1px !important; right: 0px !important; outline: 0px !important; text-shadow: initial !important; background-position: 0px 0px !important; background-repeat: initial initial !important; }
.md-inline-math .MathJax_SVG .noError { display: none !important; }
.html-for-mac .inline-math-svg .MathJax_SVG { vertical-align: 0.2px; }
.md-math-block .MathJax_SVG_Display { text-align: center; margin: 0px; position: relative; text-indent: 0px; max-width: none; max-height: none; min-height: 0px; min-width: 100%; width: auto; overflow-y: hidden; display: block !important; }
.MathJax_SVG_Display, .md-inline-math .MathJax_SVG_Display { width: auto; margin: inherit; display: inline-block !important; }
.MathJax_SVG .MJX-monospace { font-family: var(--monospace); }
.MathJax_SVG .MJX-sans-serif { font-family: sans-serif; }
.MathJax_SVG { display: inline; font-style: normal; font-weight: 400; line-height: normal; zoom: 90%; text-indent: 0px; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; }
.MathJax_SVG * { transition: none; }
.MathJax_SVG_Display svg { vertical-align: middle !important; margin-bottom: 0px !important; margin-top: 0px !important; }
.os-windows.monocolor-emoji .md-emoji { font-family: 'Segoe UI Symbol', sans-serif; }
.md-diagram-panel > svg { max-width: 100%; }
[lang="mermaid"] svg, [lang="flow"] svg { max-width: 100%; height: auto; }
[lang="mermaid"] .node text { font-size: 1rem; }
table tr th { border-bottom-width: 0px; }
video { max-width: 100%; display: block; margin: 0px auto; }
iframe { max-width: 100%; width: 100%; border: none; }
.highlight td, .highlight tr { border: 0px; }


.CodeMirror { height: auto; }
.CodeMirror.cm-s-inner { background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror-scroll { overflow-y: hidden; overflow-x: auto; z-index: 3; }
.CodeMirror-gutter-filler, .CodeMirror-scrollbar-filler { background-color: rgb(255, 255, 255); }
.CodeMirror-gutters { border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; white-space: nowrap; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror-linenumber { padding: 0px 3px 0px 5px; text-align: right; color: rgb(153, 153, 153); }
.cm-s-inner .cm-keyword { color: rgb(119, 0, 136); }
.cm-s-inner .cm-atom, .cm-s-inner.cm-atom { color: rgb(34, 17, 153); }
.cm-s-inner .cm-number { color: rgb(17, 102, 68); }
.cm-s-inner .cm-def { color: rgb(0, 0, 255); }
.cm-s-inner .cm-variable { color: rgb(0, 0, 0); }
.cm-s-inner .cm-variable-2 { color: rgb(0, 85, 170); }
.cm-s-inner .cm-variable-3 { color: rgb(0, 136, 85); }
.cm-s-inner .cm-string { color: rgb(170, 17, 17); }
.cm-s-inner .cm-property { color: rgb(0, 0, 0); }
.cm-s-inner .cm-operator { color: rgb(152, 26, 26); }
.cm-s-inner .cm-comment, .cm-s-inner.cm-comment { color: rgb(170, 85, 0); }
.cm-s-inner .cm-string-2 { color: rgb(255, 85, 0); }
.cm-s-inner .cm-meta { color: rgb(85, 85, 85); }
.cm-s-inner .cm-qualifier { color: rgb(85, 85, 85); }
.cm-s-inner .cm-builtin { color: rgb(51, 0, 170); }
.cm-s-inner .cm-bracket { color: rgb(153, 153, 119); }
.cm-s-inner .cm-tag { color: rgb(17, 119, 0); }
.cm-s-inner .cm-attribute { color: rgb(0, 0, 204); }
.cm-s-inner .cm-header, .cm-s-inner.cm-header { color: rgb(0, 0, 255); }
.cm-s-inner .cm-quote, .cm-s-inner.cm-quote { color: rgb(0, 153, 0); }
.cm-s-inner .cm-hr, .cm-s-inner.cm-hr { color: rgb(153, 153, 153); }
.cm-s-inner .cm-link, .cm-s-inner.cm-link { color: rgb(0, 0, 204); }
.cm-negative { color: rgb(221, 68, 68); }
.cm-positive { color: rgb(34, 153, 34); }
.cm-header, .cm-strong { font-weight: 700; }
.cm-del { text-decoration: line-through; }
.cm-em { font-style: italic; }
.cm-link { text-decoration: underline; }
.cm-error { color: red; }
.cm-invalidchar { color: red; }
.cm-constant { color: rgb(38, 139, 210); }
.cm-defined { color: rgb(181, 137, 0); }
div.CodeMirror span.CodeMirror-matchingbracket { color: rgb(0, 255, 0); }
div.CodeMirror span.CodeMirror-nonmatchingbracket { color: rgb(255, 34, 34); }
.cm-s-inner .CodeMirror-activeline-background { background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror { position: relative; overflow: hidden; }
.CodeMirror-scroll { height: 100%; outline: 0px; position: relative; box-sizing: content-box; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror-sizer { position: relative; }
.CodeMirror-gutter-filler, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-vscrollbar { position: absolute; z-index: 6; display: none; }
.CodeMirror-vscrollbar { right: 0px; top: 0px; overflow: hidden; }
.CodeMirror-hscrollbar { bottom: 0px; left: 0px; overflow: hidden; }
.CodeMirror-scrollbar-filler { right: 0px; bottom: 0px; }
.CodeMirror-gutter-filler { left: 0px; bottom: 0px; }
.CodeMirror-gutters { position: absolute; left: 0px; top: 0px; padding-bottom: 30px; z-index: 3; }
.CodeMirror-gutter { white-space: normal; height: 100%; box-sizing: content-box; padding-bottom: 30px; margin-bottom: -32px; display: inline-block; }
.CodeMirror-gutter-wrapper { position: absolute; z-index: 4; border: none !important; background-position: 0px 0px !important; background-repeat: initial initial !important; }
.CodeMirror-gutter-background { position: absolute; top: 0px; bottom: 0px; z-index: 4; }
.CodeMirror-gutter-elt { position: absolute; cursor: default; z-index: 4; }
.CodeMirror-lines { cursor: text; }
.CodeMirror pre { border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 0px; font-family: inherit; font-size: inherit; margin: 0px; white-space: pre; word-wrap: normal; color: inherit; z-index: 2; position: relative; overflow: visible; background-position: 0px 0px; background-repeat: initial initial; }
.CodeMirror-wrap pre { word-wrap: break-word; white-space: pre-wrap; word-break: normal; }
.CodeMirror-code pre { border-right-width: 30px; border-right-style: solid; border-right-color: transparent; width: -webkit-fit-content; }
.CodeMirror-wrap .CodeMirror-code pre { border-right-style: none; width: auto; }
.CodeMirror-linebackground { position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; z-index: 0; }
.CodeMirror-linewidget { position: relative; z-index: 2; overflow: auto; }
.CodeMirror-wrap .CodeMirror-scroll { overflow-x: hidden; }
.CodeMirror-measure { position: absolute; width: 100%; height: 0px; overflow: hidden; visibility: hidden; }
.CodeMirror-measure pre { position: static; }
.CodeMirror div.CodeMirror-cursor { position: absolute; visibility: hidden; border-right-style: none; width: 0px; }
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
.CodeMirror-focused div.CodeMirror-cursor { visibility: inherit; }
.cm-searching { background-color: rgba(255, 255, 0, 0.4); background-position: initial initial; background-repeat: initial initial; }
@media print { 
  .CodeMirror div.CodeMirror-cursor { visibility: hidden; }
}


:root {
    --side-bar-bg-color: #fafafa;
    --control-text-color: #777;
}

@include-when-export url(https://fonts.loli.net/css?family=Open+Sans:400italic,700italic,700,400&subset=latin,latin-ext);

html {
    font-size: 16px;
}

body {
    font-family: "Open Sans","Clear Sans","Helvetica Neue",Helvetica,Arial,sans-serif;
    color: rgb(51, 51, 51);
    line-height: 1.6;
}

#write {
    max-width: 860px;
  	margin: 0 auto;
  	padding: 30px;
    padding-bottom: 100px;
}
#write > ul:first-child,
#write > ol:first-child{
    margin-top: 30px;
}

a {
    color: #4183C4;
}
h1,
h2,
h3,
h4,
h5,
h6 {
    position: relative;
    margin-top: 1rem;
    margin-bottom: 1rem;
    font-weight: bold;
    line-height: 1.4;
    cursor: text;
}
h1:hover a.anchor,
h2:hover a.anchor,
h3:hover a.anchor,
h4:hover a.anchor,
h5:hover a.anchor,
h6:hover a.anchor {
    text-decoration: none;
}
h1 tt,
h1 code {
    font-size: inherit;
}
h2 tt,
h2 code {
    font-size: inherit;
}
h3 tt,
h3 code {
    font-size: inherit;
}
h4 tt,
h4 code {
    font-size: inherit;
}
h5 tt,
h5 code {
    font-size: inherit;
}
h6 tt,
h6 code {
    font-size: inherit;
}
h1 {
    padding-bottom: .3em;
    font-size: 2.25em;
    line-height: 1.2;
    border-bottom: 1px solid #eee;
}
h2 {
   padding-bottom: .3em;
    font-size: 1.75em;
    line-height: 1.225;
    border-bottom: 1px solid #eee;
}
h3 {
    font-size: 1.5em;
    line-height: 1.43;
}
h4 {
    font-size: 1.25em;
}
h5 {
    font-size: 1em;
}
h6 {
   font-size: 1em;
    color: #777;
}
p,
blockquote,
ul,
ol,
dl,
table{
    margin: 0.8em 0;
}
li>ol,
li>ul {
    margin: 0 0;
}
hr {
    height: 2px;
    padding: 0;
    margin: 16px 0;
    background-color: #e7e7e7;
    border: 0 none;
    overflow: hidden;
    box-sizing: content-box;
}

li p.first {
    display: inline-block;
}
ul,
ol {
    padding-left: 30px;
}
ul:first-child,
ol:first-child {
    margin-top: 0;
}
ul:last-child,
ol:last-child {
    margin-bottom: 0;
}
blockquote {
    border-left: 4px solid #dfe2e5;
    padding: 0 15px;
    color: #777777;
}
blockquote blockquote {
    padding-right: 0;
}
table {
    padding: 0;
    word-break: initial;
}
table tr {
    border-top: 1px solid #dfe2e5;
    margin: 0;
    padding: 0;
}
table tr:nth-child(2n),
thead {
    background-color: #f8f8f8;
}
table tr th {
    font-weight: bold;
    border: 1px solid #dfe2e5;
    border-bottom: 0;
    text-align: left;
    margin: 0;
    padding: 6px 13px;
}
table tr td {
    border: 1px solid #dfe2e5;
    text-align: left;
    margin: 0;
    padding: 6px 13px;
}
table tr th:first-child,
table tr td:first-child {
    margin-top: 0;
}
table tr th:last-child,
table tr td:last-child {
    margin-bottom: 0;
}

.CodeMirror-lines {
    padding-left: 4px;
}

.code-tooltip {
    box-shadow: 0 1px 1px 0 rgba(0,28,36,.3);
    border-top: 1px solid #eef2f2;
}

.md-fences,
code,
tt {
    border: 1px solid #e7eaed;
    background-color: #f8f8f8;
    border-radius: 3px;
    padding: 0;
    padding: 2px 4px 0px 4px;
    font-size: 0.9em;
}

code {
    background-color: #f3f4f4;
    padding: 0 2px 0 2px;
}

.md-fences {
    margin-bottom: 15px;
    margin-top: 15px;
    padding-top: 8px;
    padding-bottom: 6px;
}


.md-task-list-item > input {
  margin-left: -1.3em;
}

@media print {
    html {
        font-size: 13px;
    }
    table,
    pre {
        page-break-inside: avoid;
    }
    pre {
        word-wrap: break-word;
    }
}

.md-fences {
	background-color: #f8f8f8;
}
#write pre.md-meta-block {
	padding: 1rem;
    font-size: 85%;
    line-height: 1.45;
    background-color: #f7f7f7;
    border: 0;
    border-radius: 3px;
    color: #777777;
    margin-top: 0 !important;
}

.mathjax-block>.code-tooltip {
	bottom: .375rem;
}

.md-mathjax-midline {
    background: #fafafa;
}

#write>h3.md-focus:before{
	left: -1.5625rem;
	top: .375rem;
}
#write>h4.md-focus:before{
	left: -1.5625rem;
	top: .285714286rem;
}
#write>h5.md-focus:before{
	left: -1.5625rem;
	top: .285714286rem;
}
#write>h6.md-focus:before{
	left: -1.5625rem;
	top: .285714286rem;
}
.md-image>.md-meta {
    /*border: 1px solid #ddd;*/
    border-radius: 3px;
    padding: 2px 0px 0px 4px;
    font-size: 0.9em;
    color: inherit;
}

.md-tag {
    color: #a7a7a7;
    opacity: 1;
}

.md-toc { 
    margin-top:20px;
    padding-bottom:20px;
}

.sidebar-tabs {
    border-bottom: none;
}

#typora-quick-open {
    border: 1px solid #ddd;
    background-color: #f8f8f8;
}

#typora-quick-open-item {
    background-color: #FAFAFA;
    border-color: #FEFEFE #e5e5e5 #e5e5e5 #eee;
    border-style: solid;
    border-width: 1px;
}

/** focus mode */
.on-focus-mode blockquote {
    border-left-color: rgba(85, 85, 85, 0.12);
}

header, .context-menu, .megamenu-content, footer{
    font-family: "Segoe UI", "Arial", sans-serif;
}

.file-node-content:hover .file-node-icon,
.file-node-content:hover .file-node-open-state{
    visibility: visible;
}

.mac-seamless-mode #typora-sidebar {
    background-color: #fafafa;
    background-color: var(--side-bar-bg-color);
}

.md-lang {
    color: #b4654d;
}

.html-for-mac .context-menu {
    --item-hover-bg-color: #E6F0FE;
}

#md-notification .btn {
    border: 0;
}

.dropdown-menu .divider {
    border-color: #e5e5e5;
}

 .typora-export li, .typora-export p, .typora-export,  .footnote-line {white-space: normal;} 
</style>
</head>
<body class='typora-export' >
<div  id='write'  class = 'is-mac'><h1><a name="%E3%80%8Adjango-web%E6%A1%86%E6%9E%B6%E6%95%99%E5%AD%A6%E7%AC%94%E8%AE%B0%E3%80%8B" class="md-header-anchor"></a><span>《Django Web框架教学笔记》</span></h1><ul><li><span>讲师: 魏明择</span></li><li><span>时间: 2019</span></li></ul><h2><a name="%E7%9B%AE%E5%BD%95" class="md-header-anchor"></a><span>目录</span></h2><div class='md-toc' mdtype='toc'><p class="md-toc-content"><span class="md-toc-item md-toc-h1" data-ref="n0"><a class="md-toc-inner" href="#%E3%80%8Adjango-web%E6%A1%86%E6%9E%B6%E6%95%99%E5%AD%A6%E7%AC%94%E8%AE%B0%E3%80%8B">《Django Web框架教学笔记》</a></span><span class="md-toc-item md-toc-h2" data-ref="n8"><a class="md-toc-inner" href="#%E7%9B%AE%E5%BD%95">目录</a></span><span class="md-toc-item md-toc-h2" data-ref="n22"><a class="md-toc-inner" href="#django%E6%A1%86%E6%9E%B6%E7%9A%84%E4%BB%8B%E7%BB%8D">Django框架的介绍</a></span><span class="md-toc-item md-toc-h3" data-ref="n90"><a class="md-toc-inner" href="#django%E7%9A%84%E5%AE%89%E8%A3%85">Django的安装</a></span><span class="md-toc-item md-toc-h2" data-ref="n151"><a class="md-toc-inner" href="#django%E6%A1%86%E6%9E%B6%E5%BC%80%E5%8F%91">Django框架开发</a></span><span class="md-toc-item md-toc-h3" data-ref="n152"><a class="md-toc-inner" href="#%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE%E7%9A%84%E6%8C%87%E4%BB%A4">创建项目的指令</a></span><span class="md-toc-item md-toc-h3" data-ref="n162"><a class="md-toc-inner" href="#django%E9%A1%B9%E7%9B%AE%E7%9A%84%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84">Django项目的目录结构</a></span><span class="md-toc-item md-toc-h3" data-ref="n315"><a class="md-toc-inner" href="#url-%E4%BB%8B%E7%BB%8D">URL 介绍</a></span><span class="md-toc-item md-toc-h3" data-ref="n379"><a class="md-toc-inner" href="#%E8%A7%86%E5%9B%BE%E5%87%BD%E6%95%B0(view)">视图函数(view)</a></span><span class="md-toc-item md-toc-h3" data-ref="n397"><a class="md-toc-inner" href="#django-%E4%B8%AD%E7%9A%84%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE">Django 中的路由配置</a></span><span class="md-toc-item md-toc-h4" data-ref="n455"><a class="md-toc-inner" href="#%E5%B8%A6%E6%9C%89%E5%88%86%E7%BB%84%E7%9A%84%E8%B7%AF%E7%94%B1%E5%92%8C%E8%A7%86%E5%9B%BE%E5%87%BD%E6%95%B0">带有分组的路由和视图函数</a></span><span class="md-toc-item md-toc-h4" data-ref="n484"><a class="md-toc-inner" href="#%E5%B8%A6%E6%9C%89%E5%91%BD%E5%90%8D%E5%88%86%E7%BB%84%E7%9A%84%E8%B7%AF%E7%94%B1%E5%92%8C%E8%A7%86%E5%9B%BE%E5%87%BD%E6%95%B0">带有命名分组的路由和视图函数</a></span><span class="md-toc-item md-toc-h2" data-ref="n526"><a class="md-toc-inner" href="#http%E5%8D%8F%E8%AE%AE%E7%9A%84%E8%AF%B7%E6%B1%82%E5%92%8C%E5%93%8D%E5%BA%94">HTTP协议的请求和响应</a></span><span class="md-toc-item md-toc-h3" data-ref="n534"><a class="md-toc-inner" href="#http-%E8%AF%B7%E6%B1%82">HTTP 请求</a></span><span class="md-toc-item md-toc-h3" data-ref="n631"><a class="md-toc-inner" href="#http-%E5%93%8D%E5%BA%94">HTTP 响应</a></span><span class="md-toc-item md-toc-h3" data-ref="n746"><a class="md-toc-inner" href="#get%E6%96%B9%E5%BC%8F%E4%BC%A0%E5%8F%82">GET方式传参</a></span><span class="md-toc-item md-toc-h3" data-ref="n832"><a class="md-toc-inner" href="#post%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0">POST传递参数</a></span><span class="md-toc-item md-toc-h3" data-ref="n859"><a class="md-toc-inner" href="#form-%E8%A1%A8%E5%8D%95%E7%9A%84name%E5%B1%9E%E6%80%A7">form 表单的name属性</a></span></p></div><ul><li><p><span>课程特点：</span></p><ol start='' ><li><span>学习难度大，大部分内容需要理解并记忆</span></li><li><span>文件较多易混淆</span></li><li><span>学习阶段注重框架使用，工作阶段注重实现业务逻辑</span></li><li><span>综合应用强，小练习少</span></li></ol></li></ul><h2><a name="django%E6%A1%86%E6%9E%B6%E7%9A%84%E4%BB%8B%E7%BB%8D" class="md-header-anchor"></a><span>Django框架的介绍</span></h2><ul><li><p><span>2005年发布,采用Python语言编写的开源web框架</span></p></li><li><p><span>早期的时候Django主做新闻和内容管理的</span></p></li><li><p><span>一个重量级的 Python Web框架，Django 配备了常用的大部分组件</span></p><ol start='' ><li><span>基本配置</span></li><li><span>路由系统</span></li><li><span>原生HTML模板系统</span></li><li><span>视图 view</span></li><li><span>Model模型,数据库连接和ORM数据库管理</span></li><li><span>中间件</span></li><li><span>Cookie &amp; Seesion</span></li><li><span>分页</span></li><li><span>数据库后台管理系统admin</span></li></ol></li><li><p><span>Django的用途</span></p><ul><li><p><span>网站后端开发</span></p></li><li><p><span>微信公众号、微信小程序等后台开发</span></p></li><li><p><span>基于HTTP/HTTPS协议的后台服务器开发</span></p><ul><li><span>在线语音/图像识别服务器</span></li><li><span>在线第三方身份验证服务器等</span></li></ul></li></ul></li><li><p><span>Django的版本</span></p><ul><li><span>最新版本:2.2.x</span></li><li><span>当前教学版本:1.11.8</span></li></ul></li><li><p><span>Django的官网</span></p><ul><li><p><span>官方网址: </span><a href='http://www.djangoproject.com' target='_blank' class='url'>http://www.djangoproject.com</a></p></li><li><p><span>中文文档(第三方):</span></p><ul><li><a href='https://yiyibooks.cn/' target='_blank' class='url'>https://yiyibooks.cn/</a></li><li><a href='http://djangobook.py3k.cn/' target='_blank' class='url'>http://djangobook.py3k.cn/</a></li></ul></li><li><p><span>Django的离线文档</span></p><ol start='' ><li><span>解压缩数据包 </span><code>django-docs-1.11-en.zip</code></li><li><span>用浏览器打开 </span><code>django-docs-1.11-en/index.html</code></li></ol></li></ul></li></ul><h3><a name="django%E7%9A%84%E5%AE%89%E8%A3%85" class="md-header-anchor"></a><span>Django的安装</span></h3><ul><li><p><span>查看已安装的版本</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-operator">&gt;&gt;&gt;</span> <span class="cm-keyword">import</span> <span class="cm-variable">django</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-operator">&gt;&gt;&gt;</span> <span class="cm-builtin">print</span>(<span class="cm-variable">django</span>.<span class="cm-property">VERSION</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">(<span class="cm-number">1</span>, <span class="cm-number">11</span>, <span class="cm-number">8</span>, <span class="cm-string">'final'</span>, <span class="cm-number">0</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 110px;"></div><div class="CodeMirror-gutters" style="display: none; height: 110px;"></div></div></div></pre></li><li><p><span>安装</span></p><ol start='' ><li><p><span>在线安装</span></p><ul><li><p><code>$ sudo pip3 install django</code><span>  安装django的最新版本</span></p></li><li><p><span>或</span></p></li><li><p><code>$ sudo pip3 install django[==版本]</code><span> 安装django的指定版本</span></p></li><li><p><span>如:</span></p><ul><li><code>$ sudo pip3 install django==1.11.8</code></li></ul></li></ul></li><li><p><span>离线安装</span></p><ul><li><p><span>下载安装包:</span></p></li><li><p><span>安装离线包</span></p><ul><li><code>$ tar -xvf Django-1.11.8.tar.gz</code></li><li><code>$ cd Django-1.11.8</code></li><li><code>$ sudo python3 setup.py install</code></li></ul></li></ul></li><li><p><span>用wheel离线安装</span></p><ul><li><p><span>下载安装包:</span></p><ul><li><code>pip3 download -d /home/weimz/django_packs django==1.11.8</code></li></ul></li><li><p><span>安装离线包</span></p><ul><li><span>$ pip3 install Django-1.11.8.whl</span></li></ul></li></ul></li></ol></li><li><p><span>Django的卸载</span></p></li><li><p><span>$ pip3 uninstall django</span></p></li><li><p><span>Django 的开发环境</span></p><ul><li><span>Django 1.11.x 支持 Python 2.7, 3.4, 3.5 和 3.6（长期支持版本 LTS)</span></li><li><span>注: Django 1.11.x 不支持 Python 3.7</span></li></ul></li></ul><p>&nbsp;</p><h2><a name="django%E6%A1%86%E6%9E%B6%E5%BC%80%E5%8F%91" class="md-header-anchor"></a><span>Django框架开发</span></h2><h3><a name="%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE%E7%9A%84%E6%8C%87%E4%BB%A4" class="md-header-anchor"></a><span>创建项目的指令</span></h3><ul><li><p><span>$ django-admin startproject 项目名称</span></p></li><li><p><span>如:</span></p><ul><li><span>$ django-admin startproject mysite1</span></li></ul></li><li><p><span>运行</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="shell"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-def">$ cd</span> mysite1</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-def">$ python3</span> manage.py runserver</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 或</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-def">$ python3</span> manage.py runserver <span class="cm-number">5000</span> &nbsp;<span class="cm-comment"># 指定只能本机使用127.0.0.1的5000端口访问本机</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 176px;"></div><div class="CodeMirror-gutters" style="display: none; height: 176px;"></div></div></div></pre></li></ul><h3><a name="django%E9%A1%B9%E7%9B%AE%E7%9A%84%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84" class="md-header-anchor"></a><span>Django项目的目录结构</span></h3><ul><li><p><span>示例:</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="shell"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><span><span>​</span>x</span></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-def">$ django</span><span class="cm-attribute">-admin</span> startproject mysite1</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-def">$ tree</span> mysite1/</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">mysite1/</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">├── manage.py</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">└── mysite1</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  ├── __init__.py</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  ├── settings.py</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  ├── urls.py</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  └── wsgi.py</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-number">1</span> directory, <span class="cm-number">5</span> files</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 264px;"></div><div class="CodeMirror-gutters" style="display: none; height: 264px;"></div></div></div></pre></li><li><p><span>项目目录结构解析:</span></p><ul><li><p><span>manage.py</span></p><ul><li><p><span>此文件是项目管理的主程序,在开发阶段用于管理整个项目的开发运行的调式</span></p></li><li><p><code>manage.py</code><span> 包含项目管理的子命令, 如:</span></p><ul><li><code>python3 manage.py runserver</code><span> 启动服务</span></li><li><code>python3 manage.py startapp</code><span> 创建应用</span></li><li><code>python3 manage.py migrate</code><span> 数据库迁移</span></li><li><code>...</code></li></ul></li></ul></li><li><p><span>mysite1 项目包文件夹</span></p><ul><li><span>项目包的主文件夹(默认与项目名称一致)</span></li></ul><ol start='' ><li><p><code>__init__.py</code></p><ul><li><span>包初始化文件,当此项目包被导入(import)时此文件会自动运行</span></li></ul></li><li><p><code>wsgi.py</code></p><ul><li><span>WSGI 即 Web Server Gateway Interface</span></li><li><span>WEB服务网关接口的配置文件，仅部署项目时使用</span></li></ul></li><li><p><code>urls.py</code></p><ul><li><span>项目的基础路由配置文件，所有的动态路径必须先走该文件进行匹配</span></li></ul></li><li><p><code>settings.py</code></p><ul><li><span>Django项目的配置文件, 此配置文件中的一些全局变量将为Django框架的运行传递一些参数</span></li><li><span>setting.py 配置文件,启动服务时自动调用，</span></li><li><span>此配置文件中也可以定义一些自定义的变量用于作用全局作用域的数据传递</span></li></ul></li></ol></li></ul></li><li><p><code>settings.py</code><span> 文件介绍</span></p><ol start='' ><li><p><code>BASE_DIR</code></p><ul><li><span>用于绑定当前项目的绝对路径(动态计算出来的), 所有文件都可以依懒此路径</span></li></ul></li><li><p><code>DEBUG</code></p><ul><li><p><span>用于配置Django项目的启用模式, 取值:</span></p><ol start='' ><li><span>True 表示开发环境中使用 </span><code>调试模式</code><span>(用于开发中)</span></li><li><span>False 表示当前项目运行在</span><code>生产环境中</code><span>(不启用调试)</span></li></ol></li></ul></li><li><p><code>ALLOWED_HOSTS</code></p><ul><li><p><span>设置允许访问到本项目的网络地址列表,取值:</span></p><ol start='' ><li><span>[] 空列表,表示只有</span><code>127.0.0.1</code><span>, </span><code>localhost</code><span>, &#39;[::1]&#39; 能访问本项目</span></li><li><span>[&#39;*&#39;]，表示任何网络地址都能访问到当前项目</span></li><li><span>[&#39;*.tedu.cn&#39;, &#39;weimingze.com&#39;] 表示只有当前两个主机能访问当前项目</span></li></ol><ul><li><p><span>注意:</span></p><ul><li><span>如果要在局域网其它主机也能访问此主机,启动方式应使用如下模式:</span></li></ul></li><li><p><code>python3 manage.py runserver 0.0.0.0:5000</code><span> # 指定网络设备所有主机都可以通过5000端口访问(需加</span><code>ALLOWED_HOSTS = [&#39;*&#39;]</code><span>) </span></p></li></ul></li></ul></li><li><p><code>INSTALLED_APPS</code></p><ul><li><span>指定当前项目中安装的应用列表</span></li></ul></li><li><p><code>MIDDLEWARE</code></p><ul><li><span>用于注册中间件</span></li></ul></li><li><p><code>TEMPLATES</code></p><ul><li><span>用于指定模板的配置信息</span></li></ul></li><li><p><code>DATABASES</code></p><ul><li><span>用于指定数据库的配置信息</span></li></ul></li><li><p><code>LANGUAGE_CODE</code></p><ul><li><p><span>用于指定语言配置</span></p></li><li><p><span>取值:</span></p><ul><li><span>英文 : </span><code>&quot;en-us&quot;</code></li><li><span>中文 : </span><code>&quot;zh-Hans&quot;</code></li></ul></li></ul></li><li><p><code>TIME_ZONE</code></p><ul><li><p><span>用于指定当前服务器端时区</span></p></li><li><p><span>取值:</span></p><ul><li><span>世界标准时间: </span><code>&quot;UTC&quot;</code></li><li><span>中国时区 : </span><code>&quot;Asia/Shanghai&quot;</code></li></ul></li></ul></li><li><p><code>ROOT_URLCONF</code></p><ul><li><p><span>用于配置根级 url 配置 &#39;mysite1.urls&#39;</span></p></li><li><p><span>如:</span></p><ul><li><code>ROOT_URLCONF = &#39;mysite1.urls&#39;</code></li></ul></li></ul></li></ol><blockquote><p><span>注: 此模块可以通过 </span><code>from django.conf import settings</code><span> 导入和使用</span></p></blockquote></li></ul><h3><a name="url-%E4%BB%8B%E7%BB%8D" class="md-header-anchor"></a><span>URL 介绍</span></h3><ul><li><p><span>url 即统一资源定位符 Uniform Resource Locator</span></p></li><li><p><span>作用:</span></p><ul><li><span>用来表示互联网上某个资源的地址。</span></li></ul></li><li><p><span>说明:</span></p><ul><li><span>互联网上的每个文件都有一个唯一的URL，它包含的信息指出文件的位置以及浏览器应该怎么处理它。</span></li></ul></li><li><p><span>URL的一般语法格式为：</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang=""><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang=""><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">protocol :// hostname[:port] / path [?query][#fragment]</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 88px;"></div><div class="CodeMirror-gutters" style="display: none; height: 88px;"></div></div></div></pre></li><li><p><span>如:</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang=""><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang=""><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">http://tts.tmooc.cn/video/showVideo?menuId=657421&amp;version=AID201908#subject</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 88px;"></div><div class="CodeMirror-gutters" style="display: none; height: 88px;"></div></div></div></pre></li><li><p><span>说明:</span></p><ul><li><p><span>protocol（协议）</span></p><ul><li><span>http 通过 HTTP 访问该资源。 格式 </span><code>HTTP://</code></li><li><span>https 通过安全的 HTTPS 访问该资源。 格式 </span><code>HTTPS://</code></li><li><span>file 资源是本地计算机上的文件。格式: </span><code>file:///</code></li><li><span>...</span></li></ul></li><li><p><span>hostname（主机名）</span></p><ul><li><span>是指存放资源的服务器的域名系统(DNS) 主机名、域名 或 IP 地址。</span></li></ul></li><li><p><span>port（端口号）</span></p><ul><li><span>整数，可选，省略时使用方案的默认端口；</span></li><li><span>各种传输协议都有默认的端口号，如http的默认端口为80。</span></li></ul></li><li><p><span>path（路由地址）</span></p><ul><li><span>由零或多个“/”符号隔开的字符串，一般用来表示主机上的一个目录或文件地址。路由地址决定了服务器端如何处理这个请求</span></li></ul></li><li><p><span>query(查询)</span></p><ul><li><span>可选，用于给动态网页传递参数，可有多个参数，用“&amp;”符号隔开，每个参数的名和值用“=”符号隔开。</span></li></ul></li><li><p><span>fragment（信息片断）</span></p><ul><li><span>字符串，用于指定网络资源中的片断。例如一个网页中有多个名词解释，可使用fragment直接定位到某一名词解释。</span></li></ul></li><li><p><span>注: [] 代表其中的内容可省略</span></p></li></ul></li></ul><h3><a name="%E8%A7%86%E5%9B%BE%E5%87%BD%E6%95%B0(view)" class="md-header-anchor"></a><span>视图函数(view)</span></h3><ul><li><p><span>视图函数是用于接收一个浏览器请求并通过HttpResponse对象返回数据的函数。此函数可以接收浏览器请求并根据业务逻辑返回相应的内容给浏览器</span></p></li><li><p><span>视图处理的函数的语法格式:</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">def</span> <span class="cm-def">xxx_view</span>(<span class="cm-variable">request</span>[, <span class="cm-variable">其它参数</span>...]):</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">HttpResponse对象</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 110px;"></div><div class="CodeMirror-gutters" style="display: none; height: 110px;"></div></div></div></pre></li><li><p><span>参数:</span></p><ul><li><span>request用于绑定HttpRequest对象，通过此对象可以获取浏览器的参数和数据</span></li></ul></li><li><p><span>示例:</span></p><ul><li><p><span>视图处理函数 </span><code>views.py</code></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># file : &lt;项目名&gt;/views.py</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">from</span> <span class="cm-variable">django</span>.<span class="cm-property">http</span> <span class="cm-keyword">import</span> <span class="cm-variable">HttpResponse</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">def</span> <span class="cm-def">page1_view</span>(<span class="cm-variable">request</span>):</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">html</span> = <span class="cm-string">"&lt;h1&gt;这是第1个页面&lt;/h1&gt;"</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">HttpResponse</span>(<span class="cm-variable">html</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 264px;"></div><div class="CodeMirror-gutters" style="display: none; height: 264px;"></div></div></div></pre></li></ul></li></ul><h3><a name="django-%E4%B8%AD%E7%9A%84%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE" class="md-header-anchor"></a><span>Django 中的路由配置</span></h3><ul><li><p><span>settings.py 中的</span><code>ROOT_URLCONF</code><span> 指定了主路由配置列表urlpatterns的文件位置</span></p></li><li><p><span>urls.py 主路由配置文件</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># file : &lt;项目名&gt;/urls.py</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">urlpatterns</span> = [</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">url</span>(<span class="cm-string">r'^admin/'</span>, <span class="cm-variable">admin</span>.<span class="cm-property">site</span>.<span class="cm-property">urls</span>),</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  ... &nbsp;<span class="cm-comment"># 此处配置主路由</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">]</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 176px;"></div><div class="CodeMirror-gutters" style="display: none; height: 176px;"></div></div></div></pre><blockquote><p><span>urlpatterns 是一个路由-视图函数映射关的列表,此列表的映射关系由url函数来确定</span></p></blockquote></li></ul><ol start='3' ><li><p><span>url() 函数</span></p><ul><li><p><span>用于描述路由与视图函数的对应关系</span></p></li><li><p><span>模块</span></p><ul><li><code>from django.conf.urls import url</code></li></ul></li><li><p><span>语法:</span></p><ul><li><p><span>url(regex, views, name=None)</span></p></li><li><p><span>参数：</span></p><ol start='' ><li><span>regex: 字符串类型，匹配的请求路径，允许是正则表达式</span></li><li><span>views: 指定路径所对应的视图处理函数的名称</span></li><li><span>name: 为地址起别名，在模板中地址反向解析时使用</span></li></ol></li></ul></li></ul><blockquote><p><span>每个正则表达式前面的r表示</span><code>&#39;\&#39;</code><span>不转义的原始字符串</span></p></blockquote></li></ol><p>&nbsp;</p><ul><li><p><span>练习</span></p><ul><li><p><span>建立一个小网站:</span></p><ul><li><span>输入网址: </span><a href='http://127.0.0.1:8000' target='_blank' class='url'>http://127.0.0.1:8000</a><span>, 在网页中输出 : 这是我的首页</span></li><li><span>输入网址: </span><a href='http://127.0.0.1:8000/page1' target='_blank' class='url'>http://127.0.0.1:8000/page1</a><span>, 在网页中输出 : 这是编号为1的网页</span></li><li><span>输入网址: </span><a href='http://127.0.0.1:8000/page2' target='_blank' class='url'>http://127.0.0.1:8000/page2</a><span>, 在网页中输出 : 这是编号为2的网页</span></li></ul><blockquote><p><span>提示: 主面路由的正则是  </span><code>r&#39;^$&#39;</code></p></blockquote><ul><li><p><span>思考</span></p><ul><li><span>建立如上一百个网页该怎么办？</span></li></ul></li></ul></li></ul></li></ul><h4><a name="%E5%B8%A6%E6%9C%89%E5%88%86%E7%BB%84%E7%9A%84%E8%B7%AF%E7%94%B1%E5%92%8C%E8%A7%86%E5%9B%BE%E5%87%BD%E6%95%B0" class="md-header-anchor"></a><span>带有分组的路由和视图函数</span></h4><ul><li><p><span>在视图函数内，可以用正则表达式分组 </span><code>()</code><span> 提取参数后用函数位置传参传递给视图函数</span></p></li><li><p><span>一个分组表示一个参数,多个参数需要使用多个分组,并且使用个/隔开</span></p><ul><li><p><span>如:</span></p><ul><li><a href='http://127.0.0.1:8000/year/2018' target='_blank' class='url'>http://127.0.0.1:8000/year/2018</a></li><li><a href='http://127.0.0.1:8000/year/2019' target='_blank' class='url'>http://127.0.0.1:8000/year/2019</a></li><li><a href='http://127.0.0.1:8000/year/' target='_blank' class='url'>http://127.0.0.1:8000/year/</a><span>????  # 四位数字</span></li></ul></li></ul></li><li><p><span>练习：</span></p><ul><li><p><span>定义一个路由的格式为:</span></p><ul><li><a href='http://127.0.0.1:8000/' target='_blank' class='url'>http://127.0.0.1:8000/</a><span>整数/操作字符串/整数</span></li></ul></li><li><p><span>从路由中提取数据，做相应的操作后返回给浏览器</span></p></li><li><p><span>如：</span></p></li></ul><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang=""><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang=""><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">输入: 127.0.0.1:8000/100/add/200</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  页面显示结果：300</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">输入: 127.0.0.1:8000/100/sub/200</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  页面显示结果：-100</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">输入: 127.0.0.1:8000/100/mul/200</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  页面显示结果：20000</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 264px;"></div><div class="CodeMirror-gutters" style="display: none; height: 264px;"></div></div></div></pre></li></ul><h4><a name="%E5%B8%A6%E6%9C%89%E5%91%BD%E5%90%8D%E5%88%86%E7%BB%84%E7%9A%84%E8%B7%AF%E7%94%B1%E5%92%8C%E8%A7%86%E5%9B%BE%E5%87%BD%E6%95%B0" class="md-header-anchor"></a><span>带有命名分组的路由和视图函数</span></h4><ul><li><p><span>在url 的正则表达式中可以使用命名分组(捕获分组)</span></p></li><li><p><span>说明:</span></p><ul><li><span>在视图函数内，可以用正则表达式分组 </span><code>(?P&lt;name&gt;pattern)</code><span> 提取参数后用函数位置传参传递给视图函数</span></li></ul></li><li><p><span>示例:</span></p><ul><li><p><span>路由配置文件</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># file : &lt;项目名&gt;/urls.py</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 以下示例匹配</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># http://127.0.0.1:8000/person/weimingze/35</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># http://127.0.0.1:8000/person/shibowen/29</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># http://127.0.0.1:8000/person/xiaowei/9</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">urlpatterns</span> = [</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">url</span>(<span class="cm-string">r'^admin/'</span>, <span class="cm-variable">admin</span>.<span class="cm-property">site</span>.<span class="cm-property">urls</span>),</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">url</span>(<span class="cm-string">r'^person/(?P&lt;name&gt;\w+)/(?P&lt;age&gt;\d{1,2})'</span>,<span class="cm-variable">views</span>.<span class="cm-property">person_view</span>),</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">]</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 550px;"></div><div class="CodeMirror-gutters" style="display: none; height: 550px;"></div></div></div></pre></li></ul></li><li><p><span>练习:</span></p><ul><li><span>访问地址:</span><a href='http://127.0.0.1:8000/birthday/' target='_blank' class='url'>http://127.0.0.1:8000/birthday/</a><span>四位数字/一到两位数字/一到两位数字</span></li><li><span>最终输出: 生日为: xxxx年xx月xx日</span></li><li><span>如:</span>
<span>输入网址: </span><a href='http://127.0.0.1:8000/birthday/2015/12/11' target='_blank' class='url'>http://127.0.0.1:8000/birthday/2015/12/11</a>
<span>显示为: 生日为:2015年12月11日</span>
<span>输入网址: </span><a href='http://127.0.0.1:8000/birthday/2/28/2008' target='_blank' class='url'>http://127.0.0.1:8000/birthday/2/28/2008</a>
<span>显示为: 生日为:2008年2月28日</span></li></ul></li></ul><ul><li><p><span>PyCharm 社区版针对Django项目调试方法</span></p><ol start='' ><li><p><span>添加自己调式配置</span></p><ul><li><span>选择 Add Configuration...</span></li></ul></li><li><p><span>点击 </span><code>+</code><span> 号添加一个自己的配置</span></p><ul><li><span>选择运行的项目的主模块位置 manage.py</span></li><li><span>添加 runserver 命令行参数</span></li></ul></li></ol></li></ul><h2><a name="http%E5%8D%8F%E8%AE%AE%E7%9A%84%E8%AF%B7%E6%B1%82%E5%92%8C%E5%93%8D%E5%BA%94" class="md-header-anchor"></a><span>HTTP协议的请求和响应</span></h2><ul><li><span>请求是指浏览器端通过HTTP协议发送给服务器端的数据</span></li><li><span>响应是指服务器端接收到请求后做相应的处理后再回复给浏览器端的数据</span></li></ul><p><img src='images/request_response.png' alt='请求和向应' referrerPolicy='no-referrer' /></p><h3><a name="http-%E8%AF%B7%E6%B1%82" class="md-header-anchor"></a><span>HTTP 请求</span></h3><ul><li><p><span>根据HTTP标准，HTTP请求可以使用多种请求方法。</span></p></li><li><p><span>HTTP1.0定义了三种请求方法： GET, POST 和 HEAD方法(最常用)</span></p></li><li><p><span>HTTP1.1新增了五种请求方法：OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。</span></p></li><li><p><span>HTTP1.1 请求详述</span></p><figure><table><thead><tr><th style='text-align:center;' ><span>序号</span></th><th style='text-align:center;' ><span>方法</span></th><th style='text-align:left;' ><span>描述</span></th></tr></thead><tbody><tr><td style='text-align:center;' ><span>1</span></td><td style='text-align:center;' ><span>GET</span></td><td style='text-align:left;' ><span>请求指定的页面信息，并返回实体主体。</span></td></tr><tr><td style='text-align:center;' ><span>2</span></td><td style='text-align:center;' ><span>HEAD</span></td><td style='text-align:left;' ><span>类似于get请求，只不过返回的响应中没有具体的内容，用于获取报头</span></td></tr><tr><td style='text-align:center;' ><span>3</span></td><td style='text-align:center;' ><span>POST</span></td><td style='text-align:left;' ><span>向指定资源提交数据进行处理请求（例如提交表单或者上传文件）。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。</span></td></tr><tr><td style='text-align:center;' ><span>4</span></td><td style='text-align:center;' ><span>PUT</span></td><td style='text-align:left;' ><span>从客户端向服务器传送的数据取代指定的文档的内容。</span></td></tr><tr><td style='text-align:center;' ><span>5</span></td><td style='text-align:center;' ><span>DELETE</span></td><td style='text-align:left;' ><span>请求服务器删除指定的页面。</span></td></tr><tr><td style='text-align:center;' ><span>6</span></td><td style='text-align:center;' ><span>CONNECT</span></td><td style='text-align:left;' ><span>HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。</span></td></tr><tr><td style='text-align:center;' ><span>7</span></td><td style='text-align:center;' ><span>OPTIONS</span></td><td style='text-align:left;' ><span>允许客户端查看服务器的性能。</span></td></tr><tr><td style='text-align:center;' ><span>8</span></td><td style='text-align:center;' ><span>TRACE</span></td><td style='text-align:left;' ><span>回显服务器收到的请求，主要用于测试或诊断。</span></td></tr></tbody></table></figure></li></ul><ul><li><p><span>HttpRequest对象</span></p><ul><li><p><span>视图函数的第一个参数是HttpRequest对象</span></p></li><li><p><span>服务器接收到http协议的请求后，会根据请求数据报文创建HttpRequest对象</span></p></li><li><p><span>HttpRequest属性</span></p><ul><li><p><span>path：字符串，表示请求的路由信息</span></p></li><li><p><span>path_info: URL字符串</span></p></li><li><p><span>method：字符串，表示HTTP请求方法，常用值：&#39;GET&#39;、&#39;POST&#39;</span></p></li><li><p><span>encoding：字符串，表示提交的数据的编码方式</span></p><ul><li><span>如果为None则表示使用浏览器的默认设置，一般为&#39;utf-8&#39;</span></li><li><span>这个属性是可写的，可以通过修改它来修改访问表单数据使用的编码，接下来对属性的任何访问将使用新的encoding值</span></li></ul></li><li><p><span>GET：QueryDict查询字典的对象，包含get请求方式的所有数据</span></p></li><li><p><span>POST：QueryDict查询字典的对象，包含post请求方式的所有数据</span></p></li><li><p><span>FILES：类似于字典的对象，包含所有的上传文件信息</span></p></li><li><p><span>COOKIES：Python字典，包含所有的cookie，键和值都为字符串</span></p></li><li><p><span>session：似于字典的对象，表示当前的会话，</span></p></li><li><p><span>body: 字符串，请求体的内容(POST或PUT)</span></p></li><li><p><span>environ: 字符串,客户端运行的环境变量信息</span></p></li><li><p><span>scheme : 请求协议(&#39;http&#39;/&#39;https&#39;)</span></p></li><li><p><span>request.get_full_path() : 请求的完整路径</span></p></li><li><p><span>request.get_host() : 请求的主机</span></p></li><li><p><span>request.META : 请求中的元数据(消息头)</span></p><ul><li><span>request.META[&#39;REMOTE_ADDR&#39;]  : 客户端IP地址</span></li></ul></li></ul></li></ul></li></ul><h3><a name="http-%E5%93%8D%E5%BA%94" class="md-header-anchor"></a><span>HTTP 响应</span></h3><ul><li><p><span>当浏览者访问一个网页时，浏览者的浏览器会向网页所在服务器发出请求。当浏览器接收并显示网页前，此网页所在的服务器会返回一个包含HTTP状态码的信息头用以响应浏览器的请求。</span></p></li><li><p><span>HTTP状态码的英文为HTTP Status Code。</span></p></li><li><p><span>下面是常见的HTTP状态码：</span></p><ul><li><span>200 - 请求成功</span></li><li><span>301 - 资源（网页等）被永久转移到其它URL</span></li><li><span>404 - 请求的资源（网页等）不存在</span></li><li><span>500 - 内部服务器错误</span></li></ul></li><li><p><span>HTTP状态码分类</span></p><ul><li><p><span>HTTP状态码由三个十进制数字组成，第一个十进制数字定义了状态码的类型，后两个数字没有分类的作用。HTTP状态码共分为5种类型：</span></p><figure><table><thead><tr><th style='text-align:center;' ><span>分类</span></th><th><span>分类描述</span></th></tr></thead><tbody><tr><td style='text-align:center;' ><span>1**</span></td><td><span>信息，服务器收到请求，需要请求者继续执行操作</span></td></tr><tr><td style='text-align:center;' ><span>2**</span></td><td><span>成功，操作被成功接收并处理</span></td></tr><tr><td style='text-align:center;' ><span>3**</span></td><td><span>重定向，需要进一步的操作以完成请求</span></td></tr><tr><td style='text-align:center;' ><span>4**</span></td><td><span>客户端错误，请求包含语法错误或无法完成请求</span></td></tr><tr><td style='text-align:center;' ><span>5**</span></td><td><span>服务器错误，服务器在处理请求的过程中发生了错误</span></td></tr></tbody></table></figure></li></ul></li><li><p><span>Django中的响应对象HttpResponse:</span></p><ul><li><p><span>构造函数格式:</span></p><ul><li><code>HttpResponse(content=响应体, content_type=响应体数据类型, status=状态码)</code></li></ul></li><li><p><span>作用:</span></p><ul><li><span>向客户端浏览器返回响应，同时携带响应体内容</span></li></ul></li><li><p><span>参数:</span></p><ul><li><p><span>content：表示返回的内容。</span></p></li><li><p><span>status_code：返回的HTTP响应状态码(默认为200)。</span></p></li><li><p><span>content_type：指定返回数据的的MIME类型(默认为&quot;text/html&quot;)。浏览器会根据这个属性，来显示数据。如果是text/html，那么就会解析这个字符串，如果text/plain，那么就会显示一个纯文本。</span></p><ul><li><p><span>常用的Content-Type如下：</span></p><ul><li><code>&#39;text/html&#39;</code><span>（默认的，html文件）</span></li><li><code>&#39;text/plain&#39;</code><span>（纯文本）</span></li><li><code>&#39;text/css&#39;</code><span>（css文件）</span></li><li><code>&#39;text/javascript&#39;</code><span>（js文件）</span></li><li><code>&#39;multipart/form-data&#39;</code><span>（文件提交）</span></li><li><code>&#39;application/json&#39;</code><span>（json传输）</span></li><li><code>&#39;application/xml&#39;</code><span>（xml文件）</span></li></ul></li></ul><blockquote><p><span>注： 关键字MIME(Multipurpose Internet Mail Extensions)是指多用途互联网邮件扩展类型。</span></p></blockquote></li></ul></li></ul></li><li><p><span>HttpResponse 子类</span></p><figure><table><thead><tr><th><span>类型</span></th><th><span>作用</span></th><th><span>状态码</span></th></tr></thead><tbody><tr><td><span>HttpResponseRedirect</span></td><td><span>重定响</span></td><td><span>301</span></td></tr><tr><td><span>HttpResponseNotModified</span></td><td><span>未修改</span></td><td><span>304</span></td></tr><tr><td><span>HttpResponseBadRequest</span></td><td><span>错误请求</span></td><td><span>400</span></td></tr><tr><td><span>HttpResponseNotFound</span></td><td><span>没有对应的资源</span></td><td><span>404</span></td></tr><tr><td><span>HttpResponseForbidden</span></td><td><span>请求被禁止</span></td><td><span>403</span></td></tr><tr><td><span>HttpResponseServerError</span></td><td><span>服务器错误</span></td><td><span>500</span></td></tr></tbody></table></figure></li></ul><p>&nbsp;</p><h3><a name="get%E6%96%B9%E5%BC%8F%E4%BC%A0%E5%8F%82" class="md-header-anchor"></a><span>GET方式传参</span></h3><ul><li><p><span>GET请求方式中可以通过查询字符串(Query String)将数据传递给服务器    </span></p></li><li><p><span>URL 格式: </span><code>xxx?参数名1=值1&amp;参数名2=值2...</code></p><ul><li><span>如: </span><code>http://127.0.0.1:8000/page1?a=100&amp;b=200</code></li></ul></li><li><p><span>服务器端接收参数</span></p><ol start='' ><li><p><span>判断 request.method 的值判断请求方式是否是get请求</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">if</span> <span class="cm-variable">request</span>.<span class="cm-property">method</span> == <span class="cm-string">'GET'</span>:</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">处理GET请求时的业务逻辑</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">else</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">处理其它请求的业务逻辑</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 154px;"></div><div class="CodeMirror-gutters" style="display: none; height: 154px;"></div></div></div></pre></li><li><p><span>获取客户端请求GET请求提交的数据</span></p><ol start='' ><li><p><span>语法</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">request</span>.<span class="cm-property">GET</span>[<span class="cm-string">'参数名'</span>]</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">request</span>.<span class="cm-property">GET</span>.<span class="cm-property">get</span>(<span class="cm-string">'参数名'</span>,<span class="cm-string">'默认值'</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">request</span>.<span class="cm-property">GET</span>.<span class="cm-property">getlist</span>(<span class="cm-string">'参数名'</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 154px;"></div><div class="CodeMirror-gutters" style="display: none; height: 154px;"></div></div></div></pre></li><li><p><span>能够产生get请求方式的场合</span></p><ol start='' ><li><p><span>地址栏手动输入, 如: </span><a href='http://www.sina.com.cn/?a=100&b=200' target='_blank' class='url'>http://www.sina.com.cn/?a=100&b=200</a></p></li><li><p><code>&lt;a href=&quot;地址?参数=值&amp;参数=值&quot;&gt;</code></p></li><li><p><span>form表单中的method为get</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="html"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">form</span> <span class="cm-attribute">method</span>=<span class="cm-string">'get'</span> <span class="cm-attribute">action</span>=<span class="cm-string">"/user/login"</span><span class="cm-tag cm-bracket">&gt;</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  姓名:<span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">input</span> <span class="cm-attribute">type</span>=<span class="cm-string">"text"</span> <span class="cm-attribute">name</span>=<span class="cm-string">"uname"</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tag cm-bracket">&lt;/</span><span class="cm-tag">form</span><span class="cm-tag cm-bracket">&gt;</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 242px;"></div><div class="CodeMirror-gutters" style="display: none; height: 242px;"></div></div></div></pre></li></ol></li></ol></li></ol></li></ul><blockquote><p><span>一般查询字符串的大小会受到浏览器的的限制(不建议超过2048字节)</span></p></blockquote><ul><li><p><span>练习:</span></p><ul><li><p><span>访问地址:</span><a href='http://127.0.0.1:8000/sum?start=%E6%95%B4%E6%95%B0&stop=%E6%95%B4%E6%95%B0&step%E6%95%B4=%E5%AD%97' target='_blank' class='url'>http://127.0.0.1:8000/sum?start=整数&stop=整数&step整=字</a></p></li><li><p><span>输出结果为sum(range(start, step, stop)) 和:</span></p></li><li><p><span>如:</span></p><ul><li><span>输入网址: </span><a href='http://127.0.0.1:8000/sum?start=1&stop=101&step=1' target='_blank' class='url'>http://127.0.0.1:8000/sum?start=1&stop=101&step=1</a></li><li><span>页面显示: 结果: 5050</span></li><li><span>输入网址: </span><a href='http://127.0.0.1:8000/sum?stop=101&step=2' target='_blank' class='url'>http://127.0.0.1:8000/sum?stop=101&step=2</a></li><li><span>页面显示: 结果: 2550</span></li><li><span>输入网址: </span><a href='http://127.0.0.1:8000/sum?start=1&stop=101&step=2' target='_blank' class='url'>http://127.0.0.1:8000/sum?start=1&stop=101&step=2</a></li><li><span>页面显示: 结果: 2500</span></li></ul></li></ul></li><li><p><span>练习:</span></p><ul><li><p><span>访问地址:</span><a href='http://127.0.0.1:8000/birthday?year=%E5%9B%9B%E4%BD%8D%E6%95%B4%E6%95%B0&month=%E6%95%B4%E6%95%B0&day=%E6%95%B4%E6%95%B0' target='_blank' class='url'>http://127.0.0.1:8000/birthday?year=四位整数&month=整数&day=整数</a></p></li><li><p><span>最终输出: 生日为: xxxx年xx月xx日</span></p></li><li><p><span>如:</span></p><ul><li><span>输入网址: </span><a href='http://127.0.0.1:8000/birthday?year=2015&month=12&day=11' target='_blank' class='url'>http://127.0.0.1:8000/birthday?year=2015&month=12&day=11</a></li><li><span>显示为: 生日为:2015年12月11日</span></li></ul></li></ul></li><li><p><span>PyCharm社区版调试Django程序配置</span></p><ol start='' ><li><p><span>选择 Add Configuration</span></p></li><li><p><span>选择 左上角 + 号 选择 &quot;Python&quot;</span></p><ol start='' ><li><span>script path 里选 manage.py 的路径 </span></li><li><span>在 parameters 里填入runserver</span></li></ol></li><li><p><span>设置断点</span></p></li><li><p><span>点击开始调式按钮开始调式操作</span></p></li></ol></li></ul><h3><a name="post%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0" class="md-header-anchor"></a><span>POST传递参数</span></h3><ul><li><span>客户端通过表单等POST请求将数据传递给服务器端,如:</span></li></ul><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="html"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">form</span> <span class="cm-attribute">method</span>=<span class="cm-string">'post'</span> <span class="cm-attribute">action</span>=<span class="cm-string">"/user/login"</span><span class="cm-tag cm-bracket">&gt;</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  姓名:<span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">input</span> <span class="cm-attribute">type</span>=<span class="cm-string">"text"</span> <span class="cm-attribute">name</span>=<span class="cm-string">"username"</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tag cm-bracket">&lt;/</span><span class="cm-tag">form</span><span class="cm-tag cm-bracket">&gt;</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 132px;"></div><div class="CodeMirror-gutters" style="display: none; height: 132px;"></div></div></div></pre><ul><li><p><span>服务器端接收参数</span></p><ul><li><span>通过 request.method 来判断是否为POST请求,如:</span></li></ul><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">if</span> <span class="cm-variable">request</span>.<span class="cm-property">method</span> == <span class="cm-string">'POST'</span>:</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">处理POST请求的数据并响应</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">else</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">处理非POST</span> <span class="cm-variable">请求的响应</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 154px;"></div><div class="CodeMirror-gutters" style="display: none; height: 154px;"></div></div></div></pre></li><li><p><span>使用post方式接收客户端数据</span></p><ol start='' ><li><span>方法</span></li></ol><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">request</span>.<span class="cm-property">POST</span>[<span class="cm-string">'参数名'</span>]</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">request</span>.<span class="cm-property">POST</span>.<span class="cm-property">get</span>(<span class="cm-string">'参数名'</span>,<span class="cm-string">''</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">request</span>.<span class="cm-property">POST</span>.<span class="cm-property">getlist</span>(<span class="cm-string">'参数名'</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 110px;"></div><div class="CodeMirror-gutters" style="display: none; height: 110px;"></div></div></div></pre></li><li><p><span>取消csrf验证,否则Django将会拒绝客户端发来的POST请求</span></p><ul><li><p><span>取消 csrf 验证</span></p><ul><li><span>删除 settings.py 中 MIDDLEWARE 中的 CsrfViewsMiddleWare 的中间件</span></li></ul><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="python"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">MIDDLEWARE</span> = [</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  ...</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment"># 'django.middleware.csrf.CsrfViewMiddleware',</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  ...</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">]</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 176px;"></div><div class="CodeMirror-gutters" style="display: none; height: 176px;"></div></div></div></pre></li></ul></li></ul><h3><a name="form-%E8%A1%A8%E5%8D%95%E7%9A%84name%E5%B1%9E%E6%80%A7" class="md-header-anchor"></a><span>form 表单的name属性</span></h3><ul><li><p><span>在form表单控件提交数据时，会自动搜索本表单控件内部的子标签的name属性及相应的值，再将这些名字和值以键-值对的形式提交给action指定的服务器相关位置</span></p></li><li><p><span>在form内能自动搜集到的name属性的标签的控件有</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="html"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">input</span> <span class="cm-attribute">name</span>=<span class="cm-string">'xxx'</span><span class="cm-tag cm-bracket">&gt;</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">select</span> <span class="cm-attribute">name</span>=<span class="cm-string">'yyy'</span><span class="cm-tag cm-bracket">&gt;&lt;/</span><span class="cm-tag">select</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">textarea</span> <span class="cm-attribute">name</span>=<span class="cm-string">'zzz'</span><span class="cm-tag cm-bracket">&gt;&lt;/</span><span class="cm-tag">textarea</span><span class="cm-tag cm-bracket">&gt;</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 110px;"></div><div class="CodeMirror-gutters" style="display: none; height: 110px;"></div></div></div></pre><ul><li><span>如:</span></li></ul><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="html"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">form</span> <span class="cm-attribute">action</span>=<span class="cm-string">"/page1"</span> <span class="cm-attribute">method</span>=<span class="cm-string">"POST"</span><span class="cm-tag cm-bracket">&gt;</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">input</span> <span class="cm-attribute">name</span>=<span class="cm-string">"title"</span> <span class="cm-attribute">type</span>=<span class="cm-string">"text"</span> <span class="cm-attribute">value</span>=<span class="cm-string">"请输入"</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">select</span> <span class="cm-attribute">name</span>=<span class="cm-string">"gender"</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">option</span> <span class="cm-attribute">value</span>=<span class="cm-string">1</span><span class="cm-tag cm-bracket">&gt;</span>男<span class="cm-tag cm-bracket">&lt;/</span><span class="cm-tag">option</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">option</span> <span class="cm-attribute">value</span>=<span class="cm-string">0</span><span class="cm-tag cm-bracket">&gt;</span>女<span class="cm-tag cm-bracket">&lt;/</span><span class="cm-tag">option</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-tag cm-bracket">&lt;/</span><span class="cm-tag">select</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">textarea</span> <span class="cm-attribute">name</span>=<span class="cm-string">"comment"</span> <span class="cm-attribute">rows</span>=<span class="cm-string">"5"</span> <span class="cm-attribute">cols</span>=<span class="cm-string">"10"</span><span class="cm-tag cm-bracket">&gt;</span>附言...<span class="cm-tag cm-bracket">&lt;/</span><span class="cm-tag">textarea</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-tag cm-bracket">&lt;</span><span class="cm-tag">input</span> <span class="cm-attribute">type</span>=<span class="cm-string">"submit"</span> <span class="cm-attribute">value</span>=<span class="cm-string">"提交"</span><span class="cm-tag cm-bracket">&gt;</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tag cm-bracket">&lt;/</span><span class="cm-tag">form</span><span class="cm-tag cm-bracket">&gt;</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 462px;"></div><div class="CodeMirror-gutters" style="display: none; height: 462px;"></div></div></div></pre></li><li><p><span>小结:</span></p><ul><li><span>URL</span></li><li><span>视图</span></li><li><span>路由</span></li><li><span>从URL的路由中用正则分组提取数据</span></li><li><span>从GET请求的查询字符串中用 request.GET 获取数据</span></li><li><span>从POST请求的表单中用request.POST获取数据</span></li></ul></li></ul><p>&nbsp;</p><p>&nbsp;</p></div>
</body>
</html>